# Definition for a binary tree node.
import collections
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        # if not root.left and not root.right:
        #     return 1
        # queue = collections.deque()
        # queue.append((root,1))
        # while len(queue):
        #     node,level = queue.popleft()
        #     if not node.left and not node.right:
        #         return level
        #     if node.left:
        #         queue.append((node.left,level+1))
        #     if node.right:
        #         queue.append((node.right,level + 1))

        if not root.left:
            return 1+self.minDepth(root.right)
        elif not root.right:
            return 1 + self.minDepth(root.left)
        else:
            return 1 + min(self.minDepth(root.left), self.minDepth(root.right))









m1 = TreeNode(1)
# m1.left = TreeNode(2)
m1.right = TreeNode(2)
m1.right.left = TreeNode(3)
# m1.right.right = TreeNode(2)
s = Solution()

t = s.minDepth(m1)
print(t)
